package leetcode.offer;

import entity.TreeNode;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2021年11月21日 16:34:00
 */
public class offer54 {


    @Test
    public void test() {
        TreeNode treeNode = new TreeNode(3, new TreeNode(1, null, new TreeNode(2, null, null)), new TreeNode(4, null, null));
        int k = 1;
        System.out.println(kthLargest(treeNode, k));

    }


    public int kthLargest(TreeNode root, int k) {
        // 中序遍历
        List<Integer> list = new ArrayList<>();
        getList(root,list);
        return list.get(list.size() - k);
    }

    private void getList(TreeNode root, List<Integer> list) {
        if(root.left!=null){
            getList(root.left,list);
        }
        list.add(root.val);
        if(root.right!=null){
            getList(root.right,list);
        }

    }
}
